import { createRouter, createWebHistory } from "vue-router";
import { getToken } from "../utils/token-utils";
import NavTest from "../components/NavTest.vue";
import ContentTest from '../pages/ContentTest.vue'
import Orders from '../pages/Orders.vue'
import OrderDetail from '../pages/OrderDetail.vue'
import BedInstallation from '../pages/BedInstallation.vue'
import ConfirmOrder from '../pages/ConfirmOrder.vue'
import Login from '../pages/Login.vue'
import Register from '../pages/Register.vue'
import Image from '../pages/Image.vue'
import HomePage from '../pages/HomePage.vue'
import CancelOrder from '../pages/CancelOrder.vue'
import ModifyOrder from '../pages/ModifyOrder.vue'
import Addgwc from "../pages/Addgwc.vue"

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "navtest",
    },
    {
      name: "contentTest",
      path: "/contenttest",
      component: ContentTest,
    },
    {
        name:'addgwc',
        path:'/addgwc',
        component:Addgwc
    },
    {
      name: 'orders',
      path: '/orders',
      component: Orders
    },
    {
      name: 'orderDetail',
      path: '/orderDetail',
      component: OrderDetail,
      // props: true // 启用props接收参数
    },
    {
      name: 'confirmOrder',
      path: '/confirmOrder',
      component: ConfirmOrder,
    },
    {
      name: 'bedInstallation',
      path: '/bedInstallation',
      component: BedInstallation,
    },
    {
      name: "register",
      path: "/register",
      component: Register,
    },
    {
      path:'/NavTest',
      name: 'NavTest',
      component: NavTest
    },
    {
      name: "login",
      path: "/login",
      component: Login,
    },
    {
      name: "image",
      path: "/image",
      component: Image,
    },
      {
          name: "login",
          path: "/login",
          component: Login,
      },
      {
          name: "register",
          path: "/register",
          component: Register,
      },
      {
          name: "image",
          path: "/image",
          component: Image,
      },
      {
          name: "homepage",
          path: "/homepage",
          component: HomePage,
      },
      {
        name: 'cancelOrder',
        path: '/cancelOrder',
        component: CancelOrder
      },
      {
        name: 'modifyOrder',
        path: '/modifyOrder',
        component: ModifyOrder
      }
  ],
});

//全局前置守卫
router.beforeEach((to, from, next) => {
  const token = getToken();
  if (token) {
    if (to.path == "/login" || to.path == "/register") {
      next({ path: "/" });
    } else {
      next();
    }
  } else {
    next();
  }
});

//全局后置守卫

export default router;
